// subcontract/Home/search/search.js
var cityHistoryKey = 'cityHistoryKey'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recommend: [],//搜查发现
    historyDatas: [],//搜索历史
    keywords: '',//内容
    searchGoodsList: [],
    searchHot: []
  },
  navigateBack() {
    wx.navigateBack()
  },
  /**清空搜索记录 */
  clear() {
    wx.showModal({
      title: '提示',
      content: '您确定要清空搜索记录嘛?',
      complete: (res) => {
        if (res.confirm) {
          getApp().local.remove(cityHistoryKey)
          this.init()
        }
      }
    })
  },
  /**搜索记录 */
  async Findlists() {
    const { code, data } = await getApp().api.Findlists()
    this.setData({
      recommend: data.find_lists
    })
  },
  /**
 * 跳转
 */
  navigateToHot(event) {
    let item = event.currentTarget.dataset.item
    if (item.type == 0) {
      let url = `/subcontract/Home/searchresult/searchresult?keywords=${item.name}`
      wx.navigateTo({
        url,
      })
    }
    if (item.type == 2) {
      let url = `/subcontract/Home/storeDetails/index?id=${item.id}`
      wx.navigateTo({
        url,
      })
    }
  },
  navigateTo(event) {
    let url = event.currentTarget.dataset.url
    wx.navigateTo({
      url,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  /**
   * 搜索
   */
  async search() {
    let keywords = this.data.keywords
    if (!keywords) return getApp().showToast('请输入您要搜索的内容！')
    var historydata = getApp().local.get(cityHistoryKey)
    var historydataStr = JSON.stringify(historydata)

    if (historydataStr.indexOf(keywords) == -1) {
      if (historydata.length < 10) {
        historydata.unshift(keywords)
      } else {
        historydata.pop()
        historydata.unshift(keywords)
      }
      getApp().local.set(cityHistoryKey, historydata)
      this.setData({
        historyDatas: historydata
      })
    }
    wx.navigateTo({
      url: '/subcontract/Home/searchresult/searchresult?keywords=' + keywords,
    })
  },
  /**热搜 */
  async getSearchHot() {
    const { code, data } = await getApp().api.getSearchHot()
    if (code == 1) {
      this.setData({ searchHot: data })
    }
  },
  /**清空搜索框 */
  clearInput() {
    console.log(1);
    this.setData({
      keywords: '',
      searchGoodsList: []
    })
  },
  /**双休数据绑定 */
  model(event) {
    console.log(event.detail.value);
    let object = {
      keywords: event.detail.value
    }
    this.setData(object)
    this.searchGoods(object)
  },
  async searchGoods(object) {
    const { code, data } = await getApp().api.searchGoods(object)
    if (code == 1) {
      this.setData({ searchGoodsList: data })
    }
  },
  init() {
    var history = []
    if (getApp().local.get(cityHistoryKey)) {
      history = getApp().local.get(cityHistoryKey)
    } else {
      history = []
      getApp().local.set(cityHistoryKey, [])
    }
    this.setData({
      historyDatas: history
    })
  },
  onLoad(options) {
    this.Findlists()
    this.init()
    this.getSearchHot()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})